package com.jinyi.wwdeng.ui.steps.fragment

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager
import com.jinyi.lib_baseadapter.BGAOnItemChildClickListener
import com.jinyi.wwdeng.App
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.FragmentStepOcrBinding
import com.jinyi.wwdeng.pro.ProFragment
import com.jinyi.wwdeng.ui.steps.adapter.IdentityTypeAdapter
import com.jinyi.wwdeng.utils.recyclerview.GridSpacingItemDecoration
import org.json.JSONObject

/**
 * JinYiTest
 * @Author wwdeng
 * @CreateTime: 2024/4/19 16:49
 * @Annotation：流程 - 证件照
 */
class StepOCRFragment: ProFragment() {

    lateinit var binding: FragmentStepOcrBinding
    //类型列表
    private val typeIconList = arrayListOf(R.mipmap.pic_identity_id_card_icon,R.mipmap.pic_identity_passport_icon,R.mipmap.pic_identity_drving_license_icon)
    private val typeTitleList = arrayListOf(R.string.national_ID_card,R.string.passport,R.string.drving_license)

    //身份验证类型
    private var mTypeAdapter: IdentityTypeAdapter? = null
    override fun getRootView(inflater: LayoutInflater): View {
        binding = FragmentStepOcrBinding.inflate(inflater)
        return binding.root
    }

    override fun initView() {
        initRecyclerView()
    }

    override fun initListener() {
    }

    override fun initData() {
        val typeList = arrayListOf<JSONObject>()
        for (i in 0..<typeIconList.size){
            val obj = JSONObject()
            obj.put("isSelect",i == 0)
            obj.put("iconId",typeIconList[i])
            obj.put("title",typeTitleList[i])
            typeList.add(obj)
        }
        mTypeAdapter!!.data = typeList
        showContentView(0)
    }

    /**
     * 初始化绑卡列表
     */
    @SuppressLint("NotifyDataSetChanged")
    private fun initRecyclerView() {
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        binding.recyclerView.layoutManager = GridLayoutManager(context,3)
        mTypeAdapter = IdentityTypeAdapter(binding.recyclerView)
        val topDecoration = App.dp2px(12f)
        binding.recyclerView.addItemDecoration(GridSpacingItemDecoration(3,topDecoration,false))
        binding.recyclerView.adapter = mTypeAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        mTypeAdapter!!.setOnItemChildClickListener(BGAOnItemChildClickListener { _, childView, position ->
            if (childView.id == R.id.itemLayout){
                for (i in 0..<mTypeAdapter!!.itemCount){
                    val item = mTypeAdapter!!.getItem(i)
                    if(i == position){
                        item.put("isSelect",true)
                    } else {
                        item.put("isSelect",false)
                    }
                }
                mTypeAdapter!!.notifyDataSetChanged()
                showContentView(position)
            }
        })
    }

    /**
     * 切换类型，显示内容
     */
    private fun showContentView(index: Int){
        when(index){
            0 -> {//身份证信息
                childFragmentManager
                    .beginTransaction()
                    .replace(R.id.contentLayout, OcrIDCardFragment()) // 此处的R.id.fragment_container是要盛放fragment的父容器
                    .commitAllowingStateLoss()
            }
            1 -> {//护照信息
                childFragmentManager
                    .beginTransaction()
                    .replace(R.id.contentLayout, OcrPassportFragment()) // 此处的R.id.fragment_container是要盛放fragment的父容器
                    .commitAllowingStateLoss()
            }
            else -> {//驾驶证信息
                childFragmentManager
                    .beginTransaction()
                    .replace(R.id.contentLayout, OcrDriverLicenseFragment()) // 此处的R.id.fragment_container是要盛放fragment的父容器
                    .commitAllowingStateLoss()
            }
        }
    }

}